package com.qfedu.Action;

import com.qfedu.biz.AddressBiz;
import com.qfedu.biz.CustomerBiz;
import com.qfedu.biz.ShopCarBiz;
import com.qfedu.entity.AddressEntity;
import com.qfedu.entity.CustomerEntity;
import com.qfedu.entity.GoodsEntity;
import com.qfedu.entity.ShopCarEntity;
import com.qfedu.util.Ajaxd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CustomerAction {
    @Autowired
    private CustomerBiz customerBiz;
    @Autowired
    private ShopCarBiz shopCarBiz;
    @Autowired
    private AddressBiz addressBiz;
    //登录
    @RequestMapping("/login.do")
    @ResponseBody
    public Ajaxd doLogin(CustomerEntity customerEntity, HttpServletRequest request){

        Ajaxd ajaxd = new Ajaxd();
        CustomerEntity customerEntity1 = customerBiz.queryCustomer(customerEntity);
        if (customerEntity1==null || "".equals(customerEntity1)){
            ajaxd.setFlag(false);
        }else{
            HttpSession session = request.getSession();
            session.setAttribute("customerinfo",customerEntity1);
            ajaxd.setFlag(true);


            //查找当前用户购物车
            ShopCarEntity shopCarEntity1 = shopCarBiz.queryShopCar(customerEntity1.getCid());
            //判断当前用户是否拥有购物车,没有就添加
            if (shopCarEntity1==null){
                shopCarEntity1=new ShopCarEntity();
                shopCarEntity1.setCid(customerEntity1.getCid());
                Boolean aBoolean = shopCarBiz.insertShopCar(shopCarEntity1);
                shopCarEntity1=shopCarBiz.queryShopCar(customerEntity1.getCid());
            }
//            //查找当前session域中购物车商品

            Map<String, GoodsEntity> shopCar = (Map<String, GoodsEntity>)session.getAttribute("shopCar");
              //如果购物车有商品
            if (shopCar!=null) {
                  //得到商品id集合
                String[] strings = goodsidList(shopCar);
                int[] goodscountlist = goodscountlist(shopCar);
                Map<String,Object> map=new HashMap<>();
                map.put("scid",shopCarEntity1.getScid());
//                //添加购物车与商品映射
                for (int i = 0; i < strings.length; i++) {
                    map.put("gid",strings[i]);
                    map.put("gcount",goodscountlist[i]);
                    Boolean aBoolean1 = shopCarBiz.insertShopcarAndGoods(map);
                }

            }


//将购物车添加到session域当中
            if (shopCarEntity1.getGoodsEntityList()!=null){
                Map<String,GoodsEntity> shopmap=new HashMap<>();
                float allprice=0;
                int allcount=0;
                for (GoodsEntity goodsEntity:shopCarEntity1.getGoodsEntityList()){
                    shopmap.put(goodsEntity.getGid(),goodsEntity);
                    allprice+=goodsEntity.getGprice()*goodsEntity.getGoodscount()*goodsEntity.getGdiscount();
                    allcount+=goodsEntity.getGoodscount();
                }
                session.setAttribute("con01",allcount);
                session.setAttribute("con02",allprice);
                session.setAttribute("shopCar",shopmap);
            }


            }
        return ajaxd;
        }

//登出
    @RequestMapping("logout.do")
    public String  dologout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "login";
    }





    //遍历购物车,返回商品id集合
    private String[] goodsidList(Map<String,GoodsEntity> shopCar){
        Collection<GoodsEntity> values = shopCar.values();
        String[] str=new String[values.size()];
       int i=0;
       for (GoodsEntity goodsEntity:values){
           str[i]=goodsEntity.getGid();
           i++;
       }
       return str;
    }


//遍历购物车，返回商品个数信息
    private  int[] goodscountlist(Map<String,GoodsEntity> shopcar){
        Collection<GoodsEntity> values = shopcar.values();
       int[] goodscount=new int[shopcar.size()];
        int i=0;
        for (GoodsEntity goodsEntity:values){
            goodscount[i]=goodsEntity.getGoodscount();
            i++;
        }
        return goodscount;
    }

//注册
    @RequestMapping("/sigin.do")
    public String doSign(CustomerEntity customerEntity){
        Boolean aBoolean = customerBiz.insertCustomer(customerEntity);
            if (aBoolean){
                return "login";
            }else{
                return "error";
            }
    }

//个人
@RequestMapping("/tomyperson.do")
public String tomyperson(HttpServletRequest request,CustomerEntity customerEntity){
    AddressEntity addressEntity = new AddressEntity();
    addressEntity.setCid(customerEntity.getCid());
    CustomerEntity customerEntity1 = customerBiz.queryCustomer(customerEntity);
    List<AddressEntity> addressEntities = addressBiz.queryAddressByCid(addressEntity);
    request.setAttribute("cus",customerEntity1);
    request.setAttribute("addresslist",addressEntities);
    return "myperson";
}

//修改个人信息
    @RequestMapping("updateCus.do")
    @ResponseBody
    public CustomerEntity doupdateCus(CustomerEntity customerEntity){
        Boolean aBoolean = customerBiz.updateCus(customerEntity);
        CustomerEntity customerEntity1 = customerBiz.queryCustomer(customerEntity);
        return customerEntity1;
    }

}
